<template>
  <div class="cartsubmitbar">
    <van-submit-bar :price="price" button-text="提交订单" @submit="onSubmit">
      <van-checkbox v-model="checked" @click="check">全选</van-checkbox>
    </van-submit-bar>
  </div>
</template>

<script>
export default {
  name: "CartSubmitBar",
  data() {
    return {
      price: 0
    };
  },

  computed: {
    checked: {
      get() {
        if (this.$store.state.user) {
          return this.$store.state.user.cart_checked;
        }
      },
      set(v) {
        return v;
      }
    }
  },
  methods: {
    updateCart() {
      this.$store.dispatch("updateCart", store => {
        let price = store
          .filter(item => {
            return item.checked == true;
          })
          .reduce((preValue, n) => {
            return preValue + n.prices*n.num;
          }, 0);
        this.price = price * 100;
      });
    },
    onSubmit() {
      console.log("提交");
    },
    check() {
      this.$axios({
        url: "checkall",
        method: "post",
        data: {
          checked: !this.checked,
          uId: this.$store.state.uid
        }
      }).then(res => {
        this.$store.dispatch("updateUser");
        this.updateCart();
      });
    }
  },
  created() {
    this.$bus.$on("checkAll", checkAll => {
      
      this.$axios({
        url: "check",
        method: "post",
        data: {
          checked: checkAll,
          uId: this.$store.state.uid
        }
      }).then(res => {
        
        this.$store.dispatch("updateUser");
        this.updateCart();
      });
    });
    this.updateCart();
  }
};
</script>

<style lang="less" scoped>
.van-submit-bar {
  bottom: 40PX;
}
</style>